package com.josephxu.tank.singleton;

/**
 * @Author: Joseph Xu
 * @Date: 2022/1/23 23:05
 */
public class Mgr06 {

    private static volatile Mgr06 INSTANCE;

    //构造方法私有化
    private Mgr06(){
    }

    public static  Mgr06 getInstance() {
        if (INSTANCE == null){

            //试图通过减小同步代码块的方式提高效率，然后不可行。
            synchronized (Mgr06.class){
                //双重检查
                if (INSTANCE == null){
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    INSTANCE = new Mgr06();

                }
            }
        }

        return INSTANCE;
    }
    public void m(){
        System.out.println("m");
    }

    public static void main(String[] args) {

        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                System.out.println(Mgr06.getInstance().hashCode());
            }).start();
        }

    }
}
